package com.bdqn.demo04;

import net.sf.cglib.proxy.Enhancer;

/**
 * CGLIBProxyFactory
 *
 * @author LILIBO
 * @since 2021-12-02
 */
public class CGLIBProxyFactory {

    private static CGLIBMethodInterceptor callback = new CGLIBMethodInterceptor();

    public static <T> T create(Class<T> target) {
        Enhancer enhancer = new Enhancer();
        enhancer.setCallback(callback); // 为重写的方法指定回调的MethodInterceptor
        enhancer.setSuperclass(target); // 指定要继承的父类类型，即需要被代理的类型
        return (T) enhancer.create(); // 动态生成子类，创建子类的实例并返回
    }
}
